<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * 系统角色数据模型
 * @package App\Models
 */
class SysRole extends Model
{
    use SoftDeletes;
    //主键
    protected $primaryKey = 'role_id';
    protected $dateFormat = 'Y-m-d H:i:s';
    /**
     * 查看角色权限列表
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function permissions()
    {
        return $this->belongsToMany('App\Models\SysPermission','sys_permission_roles','role_id','perm_id')->withTimestamps();
    }

    /**
     * 给角色添加权限
     * @param $permission
     * @return Model
     */
    public function givePermissionTo($permission)
    {
        return $this->permissions()->save($permission);
    }

    /**
     * 角色下的用户列表
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function users()
    {
        return $this->belongsToMany('App\Models\SysUser','sys_role_users','role_id','user_id')->withTimestamps();
    }
}
